//
//  ScanViewController.m
//  Restaurant
//
//  Created by wanghuan on 15/4/21.
//  Copyright (c) 2015年 wanghuan. All rights reserved.
//

#import "ScanViewController.h"


@interface ScanViewController () {

    //扫描区域
    UIView *_scanView;
    
    //设置扫描区域的ui
    ScanView *_HeadScanView;
    UIImageView *_LineimageView;
    ZBarReaderViewController *reader;
   
}

@end

@implementation ScanViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
   
    self.view.backgroundColor = AppBackGroundColor;
    _manager = [[AFHTTPRequestOperationManager alloc]init];
    
}

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
   
}


#pragma mark - create zbar controller

-(void)createZbarReaderController
{
    reader = [ZBarReaderViewController new];
    
//    reader.is_Anmotion = NO;

//设置模糊层
    BackGroundScanView *backView = [[BackGroundScanView alloc]initWithFrame:CGRectMake(0, 64, APPWith, APPHeight-64)];
    
    backView.backgroundColor = [UIColor clearColor];
    
    [reader.view addSubview:backView];
    
//设置四个角
    ScanView *scanViewBorder = [[ScanView alloc]initWithFrame:CGRectMake(60-4, 150+64-4, APPWith-56*2, APPWith-56*2)];
    
    scanViewBorder.backgroundColor = [UIColor clearColor];
    
    [reader.view addSubview:scanViewBorder];
    
    
    reader.view.backgroundColor = [UIColor clearColor];
    
    reader.readerView.frame = CGRectMake(0, 0, APPWith, APPHeight);
    
//   NSLog(@"111111==%f",reader.readerView.frame.size.height);
    
//添加扫描区域
    _scanView = [[UIView alloc]initWithFrame:CGRectMake(60 , 150+64 , APPWith-120 , APPWith-120)];
//设置扫描区域范围
    reader.scanCrop = CGRectMake((_scanView.frame.origin.x-20)/APPWith, (_scanView.frame.origin.y-20)/APPHeight, (_scanView.frame.size.width+40)/APPWith,(_scanView.frame.size.height+40)/APPHeight);

    [_scanView setBackgroundColor:[UIColor clearColor]];

    [reader.readerView addSubview:_scanView];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(100, 64+50, APPWith-100*2, 60)];
    
    label.numberOfLines = 2;
    
    label.text = @"将取景框对准二维码，即可自动扫描";
    label.textColor = [UIColor whiteColor];
    label.textAlignment = NSTextAlignmentCenter;
    
    [reader.view addSubview:label];
    
    reader.showsZBarControls = NO;
    
    reader.readerDelegate = self;
    
    reader.readerView.torchMode = NO;
    
    reader.tracksSymbols = NO;
    
    reader.readerView.allowsPinchZoom = NO;
    //支持所有屏幕方向
    reader.supportedOrientationsMask = ZBarOrientationMaskAll;
    
    ZBarImageScanner *scanner = reader.scanner;
    
    [scanner setSymbology: ZBAR_I25
                   config: ZBAR_CFG_ENABLE
                       to: 0];
    
    [self.navigationController pushViewController:reader animated:YES];
    
    self.tabBarController.tabBar.hidden = YES;
    
    [self loopDrawLine];
    
    [self createNavigationBar];
    
}

#pragma mark - 创建ZBarReaderViewController的NavigationBar
- (void)createNavigationBar {

    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, APPWith, 64)];
    
    view.backgroundColor = [UIColor colorWithRed:1.0 green:0 blue:60.0/255.0 alpha:1];
    
    UIButton *LeftBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 25, 81, 32)];
    
    [LeftBtn setImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    LeftBtn.imageEdgeInsets = UIEdgeInsetsMake(0, -50, 0, 0);
    
    [LeftBtn addTarget:self action:@selector(LeftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:LeftBtn];
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake((APPWith-100)/2, 25, 100, 30)];
    
    label.text = @"扫一扫";
    
    label.font = [UIFont boldSystemFontOfSize:20];
    
    label.textAlignment = NSTextAlignmentCenter;
    
    label.textColor = [UIColor whiteColor];
    
    [view addSubview:label];
    
    UIButton *RightBtn = [[UIButton alloc]initWithFrame:CGRectMake(APPWith-60, 25, 40, 30)];
    
//    [RightBtn setBackgroundImage:[UIImage imageNamed:@"back.png"] forState:UIControlStateNormal];
    
    [RightBtn setTitle:@"完成" forState:UIControlStateNormal];
    
    [RightBtn addTarget:self action:@selector(RightClick) forControlEvents:UIControlEventTouchUpInside];
    
    [view addSubview:RightBtn];
    
    
    [reader.view addSubview:view];
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(loopDrawLine) name:@"loopDrawLine" object:nil];


}

-(void)loopDrawLine
{
    
    if (_LineimageView) {
        
        [_LineimageView removeFromSuperview];
        
    }
    _LineimageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 5, _scanView.frame.size.width, 10)];
    _LineimageView.image = [UIImage imageNamed:@"qrcode_scan_light_green"];
    [UIView animateWithDuration:3.0
                          delay: 0.0
                        options: UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         //修改fream的代码写在这里
                         _LineimageView.frame = CGRectMake(0, _scanView.frame.size.height-5, _scanView.frame.size.width, 10);
                         [_LineimageView setAnimationRepeatCount:0];
                         
                     }
                     completion:^(BOOL finished){
                         if (!reader.is_Anmotion) {
                         
                             [self loopDrawLine];
                             
//                              NSLog(@"%lu",reader.is_Anmotion);
                         }
                         
                     }];
    
    [_scanView addSubview:_LineimageView];
    
}
#pragma mark - zBar delegate method

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    //二维码扫描的信息
    
    ZBarSymbolSet *symbols = [info objectForKey:ZBarReaderControllerResults];
    
    const zbar_symbol_t *symbol = zbar_symbol_set_first_symbol(symbols.zbarSymbolSet);
    NSString *symbolStr = [NSString stringWithUTF8String: zbar_symbol_get_data(symbol)];
    
    [self RightClick:symbolStr];
    
//    MealViewController *mvc = [[MealViewController alloc]init];
//    
//    mvc.merchantId = symbolStr;
//    
//    mvc.loopDrawLine = ^{
//        
//        [self loopDrawLine];
//        
//    };
//    [self.navigationController pushViewController:mvc animated:YES];
    
    if (zbar_symbol_get_type(symbol) == ZBAR_QRCODE) {
        // 是否QR二维码
        
        NSLog(@"%@",symbolStr);// "merchantId":"26771"
        
       
        
    }
    
}

- (void)RightClick {
    
    MealViewController *mvc = [[MealViewController alloc]init];
    mvc.loopDrawLine = ^{
        
        [self loopDrawLine];
        
    };
    mvc.merchantId = @"9446";
    
    [self.navigationController pushViewController:mvc animated:YES];
    
}


- (void)RightClick:(NSString *)symbolStr {

    MealViewController *mvc = [[MealViewController alloc]init];
    mvc.loopDrawLine = ^{
    
        [self loopDrawLine];
    
    };
    mvc.merchantId = symbolStr;
    
    [self.navigationController pushViewController:mvc animated:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma NavigationBar返回按钮点击事件
- (void)LeftBtnClick {

    if (reader) {
        [reader.navigationController popViewControllerAnimated:YES];
    }

}


-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    
}

- (IBAction)buttonClick:(UIButton *)sender {

    [self createZbarReaderController];

}

@end
